package org.digitarts.rtms.spring.support.jmx

import javax.management.remote.JMXServiceURLimport javax.management.remote.JMXConnectorFactoryimport javax.management.remote.JMXConnectorimport sun.tools.jconsole.LocalVirtualMachine

/**
 * @author zhanghuabin
 *
 */
class JMXFactory
{


	static getLocalJMXConnector(String pattern)
	{
		assert pattern != null && pattern.length() > 0
		def allVms = LocalVirtualMachine.allVirtualMachines.values()
		def lvm = allVms.find
		{
			if (it.toString() =~ pattern)
			{
				return it
			}
		}
		if (lvm != null)
		{
			if (!lvm.manageable)
			{
				lvm.startManagementAgent()
				if(!lvm.manageable)
				{
					throw new IOException("$lvm not manageable")
				}
			}
			def jmxUrl = new JMXServiceURL(lvm.connectorAddress())
			return JMXConnectorFactory.connect(jmxUrl)
		}
	}


}